package com.cskaoyan;

import com.cskaoyan.bean.Cskaoyan;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

/**
 * 创建日期: 2022/06/09 10:54
 *
 * @author ciggar
 */
public class MybatisMain {

    public static void main(String[] args) throws IOException {

        // 构建SQLSessionFactory

        // 1. 获取主配置文件的输入流
        //InputStream inputStream = MybatisMain.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");


        // 2. 获取SQLSessionFactoryBuilder  XxxBuilder 建造者 建造者模式，建造者模式的目的是为了去创建对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        // 3. 获取SQLSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(stream);

        // 4. 获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        Connection connection = sqlSession.getConnection();
//
//        connection.commit();
//        sqlSession.commit();

        // 5. 执行SQL语句
        Cskaoyan cskaoyan = sqlSession.selectOne("cskaoyan.selectCskaoyanById", 1004);

        // 6. 打印结果
        System.out.println(cskaoyan);

        // 7. 关闭资源
        sqlSession.close();

    }
}
